package com.cheer.dto;

import com.cheer.entity.Admin;
import com.cheer.entity.Organization;
import com.cheer.exception.ServiceException;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;


@Data
public class AdminImportDTO {

    private static Map<String, String> headerAlias = Maps.newHashMap();

    static {
        headerAlias.put("部门名称", "orgName");
        headerAlias.put("负责人", "contactPerson");
        headerAlias.put("账号", "phone");
        headerAlias.put("备用账号", "backup");
    }

    private String orgName;

    private String contactPerson;

    private String phone;

    private String backup;

    public static Map<String, String> getHeaderAlias() {
        return headerAlias;
    }

    public AccountDTO build(Organization organization) {
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setPhone(phone);
        accountDTO.setBackupAccount(backup);
        accountDTO.setRole(organization.getOrgRole());
        accountDTO.setSecurityQuestion("管理员负责人叫什么？");
        accountDTO.setSecurityAnswer(contactPerson);
        return accountDTO;
    }

    public Admin build(Long accountId, Organization organization) {
        Admin admin = new Admin();
        admin.setAccountId(accountId);
        admin.setContactPerson(contactPerson);
        admin.setOrgId(organization.getId());
        admin.setOrgPath(organization.getOrgPath());
        return admin;
    }

    public void check(Integer index) {
        orgName = StringUtils.trimToNull(orgName);
        if (orgName == null) {
            throw new ServiceException("第" + index + "条数据的组织机构为空");
        }
        contactPerson = StringUtils.trimToNull(contactPerson);
        if (contactPerson == null) {
            throw new ServiceException("第" + index + "条数据的负责人为空");
        }
        phone = StringUtils.trimToNull(phone);
        if (phone == null) {
            throw new ServiceException("第" + index + "条数据的手机号为空");
        }
        backup = StringUtils.trimToNull(backup);
        if (backup == null) {
            throw new ServiceException("第" + index + "条数据的备用账号为空");
        }
    }
}
